﻿/* 
 * Thông tin
 * done
 */
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using DTO;
using BLL;
using DevComponents.DotNetBar;

namespace GUI.View
{
    public partial class ThongTin : UserControl
    {
        #region Khai bao
        private readonly LoaiDiaOcBLL loaidiaocBLL;
        private readonly BaoBLL baoBLL;
        private readonly ChiTietToBuomBLL chitiettobuomBLL;
        private readonly LoaiBangBLL loaibangBLL;
        private readonly ChiTietBangBLL chitietbangBLL;
        private readonly ChiTietBaoBLL chitietbaoBLL;
        private readonly ViTriBLL vitriBLL;
        private readonly MauSacBLL mausacBLL;
        private readonly KichCoBangBLL kichcoBLL;
        private readonly SoLuongToBuomBLL soluongtobuomBLL;

        #endregion
        private AppCommands _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public AppCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    AppCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(AppCommands oldValue, AppCommands newValue)
        {
            if (newValue != null)
            {
                btnCancel.Command = newValue.ThongTinCommands.Cancel;
            }
            else
            {
                btnCancel.Command = null;
            }
        }

        public ThongTin()
        {
            InitializeComponent();
            loaidiaocBLL = new LoaiDiaOcBLL();
            baoBLL = new BaoBLL();
            chitiettobuomBLL = new ChiTietToBuomBLL();
            loaibangBLL = new LoaiBangBLL();
            chitietbaoBLL = new ChiTietBaoBLL();
            vitriBLL = new ViTriBLL();
            mausacBLL = new MauSacBLL();
            chitietbangBLL = new ChiTietBangBLL();
            kichcoBLL = new KichCoBangBLL();
            soluongtobuomBLL = new SoLuongToBuomBLL();
        }

        #region LOAD lên các Gridview
        public void loaddanhsachSoLuong()
        {
            dataGridSoLuong.DataSource = soluongtobuomBLL.GetAll().Tables[0];
            for (int i = 0; i < dataGridSoLuong.RowCount - 1; i++)
            {
                dataGridSoLuong.Rows[i].Cells[0].Value = i + 1;
            }
            dataGridSoLuong.Refresh();
        }
        public void loaddanhsachKichCo()
        {
            dataGridKichCo.DataSource = kichcoBLL.GetAll().Tables[0];
            for (int i = 0; i < dataGridKichCo.RowCount - 1; i++)
            {
                dataGridKichCo.Rows[i].Cells[0].Value = i + 1;
            }
            dataGridKichCo.Refresh();
        }
        public void loaddanhsachLoaiDiaOc()
        {
            dataGridLoaiDiaOc.DataSource = loaidiaocBLL.GetAll().Tables[0];
            for (int i = 0; i < dataGridLoaiDiaOc.RowCount - 1; i++)
            {
                dataGridLoaiDiaOc.Rows[i].Cells[0].Value = i + 1;
            }
            dataGridLoaiDiaOc.Refresh();
        }

        public void loaddanhsachBao()
        {
            dataGridBao.DataSource = baoBLL.GetAll().Tables[0];
            for (int i = 0; i < dataGridBao.RowCount - 1; i++)
            {
                dataGridBao.Rows[i].Cells[0].Value = i + 1;
            }
            dataGridBao.Refresh();
        }

        public void loadchitietbao()
        {
            //const string query = "select MaChiTietBao,TenBao,TenMauSac,TenViTri,GiaTienBao from BAO,CHITIETBAO,VITRI,MAUSAC where BAO.MaBao=CHITIETBAO.MaBao and CHITIETBAO.MaMauSac=MAUSAC.MaMauSac and CHITIETBAO.MaViTri=VITRI.MaViTri";
            //if (myQueryDA.doQuery(query) == null)
            //{
            //    MessageBox.Show("NULL");
            //    return;
            //}

            DataSet ds = chitietbaoBLL.Load();
            if (ds == null)
            {
                MessageBox.Show("NULL");
                return;
            }
            else
            {
                DataTable dt = ds.Tables[0];
                dataGridChiTietBao.DataSource = dt;

                for (int i = 0; i < dataGridChiTietBao.Rows.Count - 1; i++)
                {
                    dataGridChiTietBao.Rows[i].Cells[0].Value = i + 1;
                }
                dataGridChiTietBao.Refresh();
            }
        }


        public void loaddanhsachBang()
        {
            dataGridBang.DataSource = loaibangBLL.GetAll().Tables[0];
            for (int i = 0; i < dataGridBang.RowCount - 1; i++)
            {
                dataGridBang.Rows[i].Cells[0].Value = i + 1;
            }
            dataGridBang.Refresh();
        }

        public void loadchitietBang()
        {
            //const string query = "select MaChiTietBang,TenLoaiBang,KichCo,DonViTinh,GiaTienBang from LOAIBANG,CHITIETBANG,KICHCOBANG where LOAIBANG.MaLoaiBang=CHITIETBANG.MaLoaiBang and CHITIETBANG.MaKichCo=KICHCOBANG.MaKichCo";
            //if (myQueryDA.doQuery(query) == null)
            //{
            //    MessageBox.Show("NULL");
            //    return;
            //}

            DataSet ds = chitietbangBLL.Load();
            if (ds == null)
            {
                MessageBox.Show("NULL");
                return;
            }


            DataTable dt = ds.Tables[0];
            dataGridChiTietBang.DataSource = dt;

            for (int i = 0; i < dataGridChiTietBang.Rows.Count - 1; i++)
            {

                dataGridChiTietBang.Rows[i].Cells[0].Value = i + 1;

            }
            dataGridChiTietBang.Refresh();
        }

        public void loadchitietToBuom()
        {
            //const string query = "select MaChiTietToBuom,SoLuong,GiaTienToBuom from CHITIETTOBUOM,SOLUONGTOBUOM where CHITIETTOBUOM.MaSoLuong=SOLUONGTOBUOM.MaSoLuong";
            //if (myQueryDA.doQuery(query) == null)
            //{
            //    MessageBox.Show("NULL");
            //    return;
            //}
            //DataTable dt = myQueryDA.doQuery(query).Tables[0];

            DataSet ds = chitiettobuomBLL.Load();
            if (ds == null)
            {
                MessageBox.Show("NULL");
                return;
            }
            else
            {
                DataTable dt = ds.Tables[0];
                dataGridToBuom.DataSource = dt;

                for (int i = 0; i < dataGridToBuom.Rows.Count - 1; i++)
                {

                    dataGridToBuom.Rows[i].Cells[0].Value = i + 1;

                }
                dataGridToBuom.Refresh();
            }
        }

        public void loaddanhsachViTri()
        {
            dataGridViTri.DataSource = vitriBLL.GetAll().Tables[0];
            for (int i = 0; i < dataGridViTri.RowCount - 1; i++)
            {
                dataGridViTri.Rows[i].Cells[0].Value = i + 1;
            }
            dataGridViTri.Refresh();
        }

        public void loaddanhsachMauSac()
        {
            dataGridMauSac.DataSource = mausacBLL.GetAll().Tables[0];
            for (int i = 0; i < dataGridMauSac.RowCount - 1; i++)
            {
                dataGridMauSac.Rows[i].Cells[0].Value = i + 1;
            }
            dataGridMauSac.Refresh();
        }
        #endregion

        #region địa ốc
        /*
        private void btnAddToBuom_Click(object sender, EventArgs e)
        {
            if (txtTenLoaiDiaOc.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            LoaiDiaOc loaidiaoc = new LoaiDiaOc(txtTenLoaiDiaOc.Text.Trim());
            if (loaidiaocDA.addDiaOc(loaidiaoc) > 0)
            {
                ToastNotification.Show(this, "Successful1", eToastPosition.MiddleCenter);
                loaddanhsachLoaiDiaOc();
                return;
            }

            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnEditToBuom_Click(object sender, EventArgs e)
        {
            if (txtTenLoaiDiaOc.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            LoaiDiaOc loaidiaoc = new LoaiDiaOc(Int32.Parse(txtMaLoaiDiaOc.Text.Trim()), txtTenLoaiDiaOc.Text.Trim());
            if (loaidiaocDA.updateDiaOc(loaidiaoc))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachLoaiDiaOc();
                return;
            }

            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnReFreshToBuom_Click(object sender, EventArgs e)
        {
            txtMaLoaiDiaOc.Text = "";
            txtTenLoaiDiaOc.Text = "";
            btnAddLoaiDiaOc.Enabled = true;
            btnEditLoaiDiaOc.Enabled = false;
            btnDeleteLoaiDiaOc.Enabled = false;
        }

        private void btnDeleteTomBuom_Click(object sender, EventArgs e)
        {
            if (loaidiaocDA.deleteDiaOc(Convert.ToInt32(txtMaLoaiDiaOc.Text.Trim())))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachLoaiDiaOc();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }
       

        private void dataGridToBuom_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
            try
            {

                if (i > -1 && i < dataGridLoaiDiaOc.RowCount)
                {
                    txtMaLoaiDiaOc.Text = dataGridLoaiDiaOc.Rows[i].Cells["MaLoaiDiaOc"].Value.ToString().Trim();
                    txtTenLoaiDiaOc.Text = dataGridLoaiDiaOc.Rows[i].Cells["TenLoaiDiaOc"].Value.ToString().Trim();
                    btnAddLoaiDiaOc.Enabled = false;
                    btnEditLoaiDiaOc.Enabled = true;
                    btnDeleteLoaiDiaOc.Enabled = true;
                }
            }
            catch (Exception ie)
            {
                MessageBox.Show("Lỗi Rồi");
                Console.WriteLine(ie.Message);
            }

        }
         * */
        #endregion

        private void TabControl_ThongTin_SelectedTabChanged(object sender, SuperTabStripSelectedTabChangedEventArgs e)
        {
            if (TabControl_ThongTin.SelectedTabIndex == 0)
            {
                txtMaToBuom.Text = MaToBuomTT().ToString();
                cbbSoLuong.DataSource = soluongtobuomBLL.GetAll().Tables[0];
                cbbSoLuong.DisplayMember = "SoLuong";
                cbbSoLuong.ValueMember = "MaSoLuong";
                loadchitietToBuom();

            }
            else
                if (TabControl_ThongTin.SelectedTabIndex == 1)
                {
                    txtMaBang.Text = MaLoaiBangTT().ToString();
                    loaddanhsachBang();
                }
                else
                    if (TabControl_ThongTin.SelectedTabIndex == 2)
                    {
                        txtMaChiTietBang.Text = MaChiTietBangTT().ToString();
                        cbbTenBang.DataSource = loaibangBLL.GetAll().Tables[0];
                        cbbTenBang.DisplayMember = "TenLoaiBang";
                        cbbTenBang.ValueMember = "MaLoaiBang";
                        cbbKichCo.DataSource = kichcoBLL.GetAll().Tables[0];
                        cbbKichCo.DisplayMember = "KichCo";
                        cbbKichCo.ValueMember = "MaKichCo";
                        loadchitietBang();
                    }
                    else
                        if (TabControl_ThongTin.SelectedTabIndex == 3)
                        {
                            txtMaBao.Text = MaBaoTT().ToString();
                            loaddanhsachBao();
                            lblTongSoToBao.Text = dataGridChiTietBao.Rows.Count - 1 + " Tờ Báo";
                        }
                        else
                            if (TabControl_ThongTin.SelectedTabIndex == 4)
                            {
                                txtMaChiTietBao.Text = MaChiTietBaoTT().ToString();
                                cbbTenBao.DataSource = baoBLL.GetAll().Tables[0];
                                cbbTenBao.DisplayMember = "TenBao";
                                cbbTenBao.ValueMember = "MaBao";
                                cbMauSac.DataSource = mausacBLL.GetAll().Tables[0];
                                cbMauSac.DisplayMember = "TenMauSac";
                                cbMauSac.ValueMember = "MaMauSac";
                                cbViTri.DataSource = vitriBLL.GetAll().Tables[0];
                                cbViTri.DisplayMember = "TenViTri";
                                cbViTri.ValueMember = "MaViTri";
                                loadchitietbao();
                            }
                            else
                                if (TabControl_ThongTin.SelectedTabIndex == 5)
                                {
                                    txtMaLoaiDiaOc.Text = MaLoaiDiaOcTT().ToString();
                                    loaddanhsachLoaiDiaOc();
                                    lblTongSoLoaiDiaOc.Text = dataGridLoaiDiaOc.Rows.Count - 1 + " Loại Địa Ốc";
                                }
                                else
                                    if (TabControl_ThongTin.SelectedTabIndex == 6)
                                    {
                                        txtMaViTri.Text = MaViTriBaoTT().ToString();
                                        loaddanhsachViTri();
                                    }
                                    else
                                        if (TabControl_ThongTin.SelectedTabIndex == 7)
                                        {
                                            txtMaMauSac.Text = MaMauSacBaoTT().ToString();
                                            loaddanhsachMauSac();
                                        }
                                        else
                                            if (TabControl_ThongTin.SelectedTabIndex == 8)
                                            {
                                                txtMaKichCo.Text = MaKichCoBangTT().ToString();
                                                loaddanhsachKichCo();
                                            }
                                            else
                                            {
                                                txtMaSoLuong.Text = MasoluongtobuomTT().ToString();
                                                loaddanhsachSoLuong();
                                            }

        }

        #region  CHI TIẾT BÁO
        private void btnAddBao_Click(object sender, EventArgs e)
        {
            if (cbbTenBao.Text.Equals("") || cbMauSac.Text.Equals("") || cbViTri.Text.Equals("") || txtGiaTienBao.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            ChiTietBaoDTO chitietbao = new ChiTietBaoDTO(Convert.ToInt32(cbMauSac.SelectedValue.ToString()), Convert.ToInt32(cbViTri.SelectedValue.ToString()), Int32.Parse(txtGiaTienBao.Text.Trim()), Int32.Parse(cbbTenBao.SelectedValue.ToString()));
            if (chitietbaoBLL.Add(chitietbao) > 0)
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loadchitietbao();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnEditBao_Click(object sender, EventArgs e)
        {
            if (cbbTenBao.Text.Equals("") || cbMauSac.Text.Equals("") || cbViTri.Text.Equals("") || txtGiaTienBao.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            ChiTietBaoDTO chitietbao = new ChiTietBaoDTO(Int32.Parse(txtMaChiTietBao.Text), Convert.ToInt32(cbMauSac.SelectedValue.ToString()), Convert.ToInt32(cbViTri.SelectedValue.ToString()), float.Parse(txtGiaTienBao.Text.Trim()), Int32.Parse(cbbTenBao.SelectedValue.ToString()));
            if (chitietbaoBLL.Update(chitietbao))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loadchitietbao();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnRefreshBao_Click(object sender, EventArgs e)
        {
            txtMaChiTietBao.Text = "";
            cbbTenBao.Text = "";
            cbViTri.Text = "";
            cbMauSac.Text = "";
            txtGiaTienBao.Text = "";
            btnAddBao.Enabled = true;
            btnEditBao.Enabled = false;
            btnDeleteBao.Enabled = false;
        }

        private void btnDeleteBao_Click(object sender, EventArgs e)
        {
            if (chitietbaoBLL.Delete(Convert.ToInt32(txtMaChiTietBao.Text.Trim())))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loadchitietbao();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void dataGridChiTietBao_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex; ;
            try
            {

                if (i > -1 && i < dataGridChiTietBao.RowCount)
                {
                    txtMaChiTietBao.Text = dataGridChiTietBao.Rows[i].Cells["MaChiTietBao"].Value.ToString().Trim();
                    cbMauSac.Text = dataGridChiTietBao.Rows[i].Cells["MauSac"].Value.ToString().Trim();
                    cbbTenBao.Text = dataGridChiTietBao.Rows[i].Cells["TenToBao"].Value.ToString().Trim();
                    cbViTri.Text = dataGridChiTietBao.Rows[i].Cells["ViTri"].Value.ToString().Trim();
                    txtGiaTienBao.Text = dataGridChiTietBao.Rows[i].Cells["GiaTienBao"].Value.ToString().Trim();
                    btnAddBao.Enabled = false;
                    btnEditBao.Enabled = true;
                    btnDeleteBao.Enabled = true;
                }
            }
            catch (Exception ie)
            {
                MessageBox.Show("Loi roi");
                Console.WriteLine(ie.Message);
            }
        }
        #endregion

        #region BÁO
        private void btnThemBao_Click(object sender, EventArgs e)
        {
            if (txtTenBao.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            BaoDTO bao = new BaoDTO(txtTenBao.Text.Trim());
            if (baoBLL.Add(bao) > 0)
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachBao();
                return;
            }

            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }


        private void btnCapNhatBao_Click(object sender, EventArgs e)
        {
            if (txtTenBang.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            BaoDTO bao = new BaoDTO(Convert.ToInt32(txtMaBao.Text.Trim()), txtTenBao.Text.Trim());
            if (!baoBLL.Update(bao))
            {
                ToastNotification.Show(this, "Fail", eToastPosition.MiddleCenter);
                return;
            }
            else
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachBao();
                return;

            }
        }

        private void btnLamMoiBao_Click(object sender, EventArgs e)
        {
            txtTenBao.Text = "";
            txtMaBao.Text = "";
            btnThemBao.Enabled = true;
            btnCapNhatBao.Enabled = false;
            btnXoaBao.Enabled = false;
        }

        private void btnXoaBao_Click(object sender, EventArgs e)
        {
            if (baoBLL.Delete(Convert.ToInt32(txtMaBao.Text.Trim())))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachBao();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void dataGridBao_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex; ;
            try
            {

                if (i > -1 && i < dataGridBao.RowCount)
                {
                    txtMaBao.Text = dataGridBao.Rows[i].Cells["MaBao"].Value.ToString().Trim();
                    txtTenBao.Text = dataGridBao.Rows[i].Cells["TenBao"].Value.ToString().Trim();
                    btnThemBao.Enabled = false;
                    btnCapNhatBao.Enabled = true;
                    btnXoaBao.Enabled = true;
                }
            }
            catch (Exception ie)
            {
                MessageBox.Show("Loi roi");
                Console.WriteLine(ie.Message);
            }
        }
        #endregion

        #region VỊ TRÍ
        private void btnThemViTri_Click(object sender, EventArgs e)
        {
            if (txtTenViTri.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            ViTriDTO vitr = new ViTriDTO(txtTenViTri.Text.Trim());
            if (vitriBLL.Add(vitr) > 0)
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachViTri();
                return;
            }

            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnCapNhatViTri_Click(object sender, EventArgs e)
        {
            if (txtTenViTri.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            ViTriDTO vitri = new ViTriDTO(Int32.Parse(txtMaViTri.Text.Trim()), txtTenViTri.Text.Trim());
            if (vitriBLL.Update(vitri))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachViTri();
                return;
            }

            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnLamMoiViTri_Click(object sender, EventArgs e)
        {
            txtMaViTri.Text = "";
            txtTenViTri.Text = "";
            btnThemViTri.Enabled = true;
            btnCapNhatViTri.Enabled = false;
            btnXoaViTri.Enabled = false;
        }

        private void btnXoaViTri_Click(object sender, EventArgs e)
        {
            if (vitriBLL.Delete(Convert.ToInt32(txtMaViTri.Text.Trim())))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachViTri();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void dataGridViTri_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex; ;
            try
            {

                if (i > -1 && i < dataGridViTri.RowCount)
                {
                    txtMaViTri.Text = dataGridViTri.Rows[i].Cells["MaViTri"].Value.ToString().Trim();
                    txtTenViTri.Text = dataGridViTri.Rows[i].Cells["TenViTri"].Value.ToString().Trim();
                    btnThemViTri.Enabled = false;
                    btnCapNhatViTri.Enabled = true;
                    btnXoaViTri.Enabled = true;
                }
            }
            catch (Exception ie)
            {
                MessageBox.Show("Loi roi");
                Console.WriteLine(ie.Message);
            }
        }
        #endregion

        #region MÀU SẮC
        private void btnThemMauSau_Click(object sender, EventArgs e)
        {
            if (txtTenMauSac.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            MauSacDTO mausac = new MauSacDTO(txtTenMauSac.Text.Trim());
            if (mausacBLL.Add(mausac) > 0)
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachMauSac();
                return;
            }

            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnCapNhatMauSac_Click(object sender, EventArgs e)
        {
            if (txtMaMauSac.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            MauSacDTO vitri = new MauSacDTO(Int32.Parse(txtMaMauSac.Text.Trim()), txtTenMauSac.Text.Trim());
            if (mausacBLL.Update(vitri))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachMauSac();
                return;
            }

            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnLamMoiMauSac_Click(object sender, EventArgs e)
        {
            txtMaMauSac.Text = "";
            txtTenMauSac.Text = "";
            btnThemMauSau.Enabled = true;
            btnCapNhatMauSac.Enabled = false;
            tbnXoaMauSac.Enabled = false;
        }

        private void tbnXoaMauSac_Click(object sender, EventArgs e)
        {
            if (mausacBLL.Delete(Convert.ToInt32(txtMaMauSac.Text.Trim())))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachMauSac();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void dataGridMauSac_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex; ;
            try
            {

                if (i > -1 && i < dataGridMauSac.RowCount)
                {
                    txtMaMauSac.Text = dataGridMauSac.Rows[i].Cells["MaMauSac"].Value.ToString().Trim();
                    txtTenMauSac.Text = dataGridMauSac.Rows[i].Cells["TenMauSac"].Value.ToString().Trim();
                    btnThemMauSau.Enabled = false;
                    btnCapNhatMauSac.Enabled = true;
                    tbnXoaMauSac.Enabled = true;
                }
            }
            catch (Exception ie)
            {
                MessageBox.Show("Loi roi");
                Console.WriteLine(ie.Message);
            }
        }
        #endregion

        #region Bảng
        //them bang
        private void btnThemBang_Click(object sender, EventArgs e)
        {
            if (txtTenBang.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            LoaiBangDTO bang = new LoaiBangDTO(txtTenBang.Text.Trim());
            if (loaibangBLL.Add(bang) > 0)
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachBang();
                return;
            }
        }

        private void btnCapNhatbang_Click(object sender, EventArgs e)
        {
            if (txtTenBang.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            LoaiBangDTO bang = new LoaiBangDTO(Convert.ToInt32(txtMaBang.Text.Trim()), txtTenBang.Text.Trim());
            if (!loaibangBLL.Update(bang))
            {
                ToastNotification.Show(this, "Fail", eToastPosition.MiddleCenter);
                return;
            }
            else
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachBang();
                return;

            }
        }

        private void btnLamMoiBang_Click(object sender, EventArgs e)
        {
            txtMaBang.Text = "";
            txtTenBang.Text = "";
            btnThemBang.Enabled = true;
            btnCapNhatbang.Enabled = false;
        }

        private void btnXoaBang_Click(object sender, EventArgs e)
        {
            if (loaibangBLL.Delete(Convert.ToInt32(txtMaBang.Text.Trim())))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachBang();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void dataGridBang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex; ;
            try
            {

                if (i > -1 && i < dataGridBang.RowCount)
                {
                    txtMaBang.Text = dataGridBang.Rows[i].Cells["MaLoaiBang"].Value.ToString().Trim();
                    txtTenBang.Text = dataGridBang.Rows[i].Cells["TenLoaiBang"].Value.ToString().Trim();
                    btnThemBang.Enabled = false;
                    btnCapNhatbang.Enabled = true;
                    btnXoaBang.Enabled = true;
                }
            }
            catch (Exception ie)
            {
                MessageBox.Show("Loi roi");
                Console.WriteLine(ie.Message);
            }
        }
        #endregion

        #region CHI TIẾT BẢNG
        private void btnAddchitietbang_Click(object sender, EventArgs e)
        {
            if (cbbTenBang.Text.Equals("") || txtGiaTienBang.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            ChiTietBangDTO chitietbang = new ChiTietBangDTO(Convert.ToInt32(cbbTenBang.SelectedValue.ToString()), Convert.ToInt32(cbbKichCo.SelectedValue.ToString()), txtDonViTinh.Text.Trim(), float.Parse(txtGiaTienBang.Text.Trim()));
            if (chitietbangBLL.Add(chitietbang) > 0)
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loadchitietBang();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnEditchitietBang_Click(object sender, EventArgs e)
        {
            if (cbbTenBang.Text.Equals("") || txtGiaTienBang.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            ChiTietBangDTO chitietbang = new ChiTietBangDTO(Convert.ToInt32(txtMaChiTietBang.Text), Convert.ToInt32(cbbTenBang.SelectedValue.ToString()), Convert.ToInt32(cbbKichCo.SelectedValue.ToString()), txtDonViTinh.Text.Trim(), float.Parse(txtGiaTienBang.Text.Trim()));
            if (chitietbangBLL.Update(chitietbang))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loadchitietBang();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnRefreshchitietBang_Click(object sender, EventArgs e)
        {
            txtKichCo.Text = "";
            txtTenBang.Text = "";
            txtMaChiTietBang.Text = "";
            txtGiaTienBang.Text = "";
            btnAddchitietbang.Enabled = true;
            btnEditchitietBang.Enabled = false;
            btnDeletechitietBang.Enabled = false;
        }

        private void btnDeletechitietBang_Click(object sender, EventArgs e)
        {
            if (chitietbangBLL.Delete(Convert.ToInt32(txtMaChiTietBang.Text.Trim())))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loadchitietBang();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void dataGridChiTietBang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex; ;
            try
            {

                if (i > -1 && i < dataGridChiTietBang.RowCount)
                {
                    txtMaChiTietBang.Text = dataGridChiTietBang.Rows[i].Cells["MaChiTietBang"].Value.ToString().Trim();
                    txtGiaTienBang.Text = dataGridChiTietBang.Rows[i].Cells["GiaTienBang"].Value.ToString().Trim();
                    cbbTenBang.Text = dataGridChiTietBang.Rows[i].Cells["LoaiBang"].Value.ToString().Trim();
                    cbbKichCo.Text = dataGridChiTietBang.Rows[i].Cells["KichCoBang"].Value.ToString().Trim();

                    btnAddchitietbang.Enabled = false;
                    btnEditchitietBang.Enabled = true;
                    btnDeletechitietBang.Enabled = true;
                }
            }
            catch (Exception ie)
            {
                MessageBox.Show("Loi roi");
                Console.WriteLine(ie.Message);
            }
        }
        #endregion

        #region Kích cỡ
        private void btnThemKichCo_Click(object sender, EventArgs e)
        {
            if (txtKichCo.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            KichCoBangDTO kichco = new KichCoBangDTO(float.Parse(txtKichCo.Text.Trim()));
            if (kichcoBLL.Add(kichco) > 0)
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachKichCo();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnCapNhatKichCo_Click(object sender, EventArgs e)
        {
            if (txtKichCo.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            KichCoBangDTO kichco = new KichCoBangDTO(Convert.ToInt32(txtMaKichCo.Text.Trim()), float.Parse(txtKichCo.Text.Trim()));
            if (!kichcoBLL.Update(kichco))
            {
                ToastNotification.Show(this, "Fail", eToastPosition.MiddleCenter);
                return;
            }
            else
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachKichCo();
                return;

            }
        }

        private void btnLamMoiKichCo_Click(object sender, EventArgs e)
        {
            txtKichCo.Text = "";
            txtMaKichCo.Text = "";
            btnXoaKichCo.Enabled = false;
            btnThemKichCo.Enabled = true;
            btnCapNhatKichCo.Enabled = false;
        }

        private void btnXoaKichCo_Click(object sender, EventArgs e)
        {
            if (kichcoBLL.Delete(Convert.ToInt32(txtMaKichCo.Text.Trim())))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachKichCo();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }
        private void dataGridKichCo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex; ;
            try
            {

                if (i > -1 && i < dataGridKichCo.RowCount)
                {
                    txtMaKichCo.Text = dataGridKichCo.Rows[i].Cells["MaKichCo"].Value.ToString().Trim();
                    txtKichCo.Text = dataGridKichCo.Rows[i].Cells["KichCo"].Value.ToString().Trim();
                    btnThemKichCo.Enabled = false;
                    btnCapNhatKichCo.Enabled = true;
                    btnXoaKichCo.Enabled = true;
                }
            }
            catch (Exception ie)
            {
                MessageBox.Show("Loi roi");
                Console.WriteLine(ie.Message);
            }
        }
        #endregion

        #region Số Lượng tờ bướm
        private void btnThemSoLuong_Click(object sender, EventArgs e)
        {
            if (txtSoLuong.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            SoLuongToBuomDTO soluong = new SoLuongToBuomDTO(Int32.Parse(txtSoLuong.Text.Trim()));
            if (soluongtobuomBLL.Add(soluong) > 0)
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachSoLuong();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnCapNhatSoLuong_Click(object sender, EventArgs e)
        {
            if (txtSoLuong.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            SoLuongToBuomDTO soluong = new SoLuongToBuomDTO(Convert.ToInt32(txtMaSoLuong.Text.Trim()), Int32.Parse(txtSoLuong.Text.Trim()));
            if (!soluongtobuomBLL.Update(soluong))
            {
                ToastNotification.Show(this, "Fail", eToastPosition.MiddleCenter);
                return;
            }
            else
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachSoLuong();
                return;

            }
        }

        private void btnXoaSoLuong_Click(object sender, EventArgs e)
        {
            if (soluongtobuomBLL.Delete(Convert.ToInt32(txtMaSoLuong.Text.Trim())))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loaddanhsachSoLuong();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnLamMoiSoLuong_Click(object sender, EventArgs e)
        {
            txtMaSoLuong.Text = "";
            txtSoLuong.Text = "";
            btnXoaSoLuong.Enabled = false;
            btnCapNhatSoLuong.Enabled = false;
            btnThemSoLuong.Enabled = true;
        }
        private void dataGridSoLuong_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex; ;
            try
            {

                if (i > -1 && i < dataGridSoLuong.RowCount)
                {
                    txtMaSoLuong.Text = dataGridSoLuong.Rows[i].Cells["MaSoLuong"].Value.ToString().Trim();
                    txtSoLuong.Text = dataGridSoLuong.Rows[i].Cells["SoLuongToBuom"].Value.ToString().Trim();
                    btnThemSoLuong.Enabled = false;
                    btnCapNhatSoLuong.Enabled = true;
                    btnXoaSoLuong.Enabled = true;
                }
            }
            catch (Exception ie)
            {
                MessageBox.Show("Loi roi");
                Console.WriteLine(ie.Message);
            }
        }
        #endregion

        #region Tờ Bướm
        private void btnAddToBuom_Click(object sender, EventArgs e)
        {
            if (cbbSoLuong.Text.Equals("") || txtGiaTienToBuom.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            ChiTietToBuomDTO chitiettobuom = new ChiTietToBuomDTO(Convert.ToInt32(cbbSoLuong.SelectedValue.ToString()), float.Parse(txtGiaTienToBuom.Text.Trim()));
            if (chitiettobuomBLL.Add(chitiettobuom) > 0)
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loadchitietToBuom();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnEditToBuom_Click(object sender, EventArgs e)
        {
            if (cbbSoLuong.Text.Equals("") || txtGiaTienToBuom.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin", eToastPosition.MiddleCenter);
                return;
            }
            ChiTietToBuomDTO chitiettobuom = new ChiTietToBuomDTO(Convert.ToInt32(txtMaToBuom.Text.Trim()), Convert.ToInt32(cbbSoLuong.SelectedValue.ToString()), float.Parse(txtGiaTienToBuom.Text.Trim()));
            if (!chitiettobuomBLL.Update(chitiettobuom))
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
            else
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loadchitietToBuom();

                return;
            }
        }

        private void btnReFreshToBuom_Click(object sender, EventArgs e)
        {
            txtMaToBuom.Text = "";
            cbbSoLuong.Text = "";
            txtGiaTienToBuom.Text = "";
            btnAddToBuom.Enabled = true;
            btnEditToBuom.Enabled = false;
        }

        private void btnDeleteToBuom_Click(object sender, EventArgs e)
        {
            if (chitiettobuomBLL.Delete(Convert.ToInt32(txtMaToBuom.Text.Trim())))
            {
                ToastNotification.Show(this, "Successful", eToastPosition.MiddleCenter);
                loadchitietToBuom();
                return;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
                return;
            }
        }
        private void dataGridToBuom_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex; ;
            try
            {

                if (i > -1 && i < dataGridToBuom.RowCount)
                {
                    txtMaToBuom.Text = dataGridToBuom.Rows[i].Cells["MaChiTietToBuom"].Value.ToString().Trim();
                    cbbSoLuong.Text = dataGridToBuom.Rows[i].Cells["SoLuong"].Value.ToString().Trim();
                    txtGiaTienToBuom.Text = dataGridToBuom.Rows[i].Cells["GiaTienToBuom"].Value.ToString().Trim();
                    btnAddToBuom.Enabled = false;
                    btnEditToBuom.Enabled = true;
                    btnDeleteTomBuom.Enabled = true;
                }
            }
            catch (Exception ie)
            {
                MessageBox.Show("Loi roi");
                Console.WriteLine(ie.Message);
            }
        }
        #endregion

        #region  LAY DANH SACH CAC LOAI MA
        public int MasoluongtobuomTT()
        {
            //const string _getMasoluong = "select MAX(MaSoLuong) from SOLUONGTOBUOM";         
            //int _masoluong = myQueryDA.getId(_getMasoluong) + 1;

            int _masoluong = soluongtobuomBLL.getMax_MaSoLuong() + 1;
            return _masoluong;
        }
        public int MaKichCoBangTT()
        {
            //const string _getMasoluong = "select MAX(MaKichCo) from KICHCOBANG";
            //int _masoluong = myQueryDA.getId(_getMasoluong) + 1;

            int _masoluong = kichcoBLL.getMax_MaKichCo() + 1;
            return _masoluong;
        }
        public int MaMauSacBaoTT()
        {
            //const string _getMasoluong = "select MAX(MaMauSac) from MauSac";
            //int _masoluong = myQueryDA.getId(_getMasoluong) + 1;

            return mausacBLL.getMax_MaMauSac() + 1;
        }
        public int MaViTriBaoTT()
        {
            return vitriBLL.getMax_MaViTri() + 1;
        }
        public int MaLoaiDiaOcTT()
        {
            return loaidiaocBLL.getMax_MaLoaiDiaOc() + 1;
        }
        public int MaChiTietBangTT()
        {
            return chitietbangBLL.getMax_MaChiTietBang() + 1;
        }
        public int MaChiTietBaoTT()
        {
            return chitietbaoBLL.getMax_MaChiTietBao() + 1;
        }
        public int MaBaoTT()
        {
            return baoBLL.getMax_MaBao() + 1;
        }
        public int MaLoaiBangTT()
        {

            return loaibangBLL.getMax_MaLoaiBang();
        }
        public int MaToBuomTT()
        {

            return new ToBuomBLL().getMax_MaToBuom() + 1;
        }
        #endregion

        #region Điều kiện nhập
        private void txtGiaTienBang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGiaTienToBuom_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGiaTienBao_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        #endregion

    }
}
